package io.github.codelifeleno.jdbrelease.execute;

import cn.hutool.core.util.URLUtil;
import io.github.codelifeleno.jdbrelease.util.JDBResourceUtils;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

public class ReaderTest {
    @Test
    void test() {
        try {
            // 1. 查找所有配置文件
            String regex = ".*.sql"; // 匹配 config 目录下的 .properties 文件
            List<String> files = JDBResourceUtils.findMatchingResources(this.getClass(), regex);
            System.out.println("匹配到的文件: " + files);

            files.forEach(e -> {
                try {
                    URL url = new URL(e);
                    System.out.println(URLUtil.isFileURL(url));
                    System.out.println(URLUtil.isJarURL(url));
                    System.out.println(URLUtil.isJarFileURL(url));
                    JDBResourceUtils.handlerResource(e, a -> {
                        BufferedReader br = new BufferedReader(new InputStreamReader(a, Charset.defaultCharset()));
                        br.lines().forEach(System.out::println);
                    });
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });

            // 3. 提取文件到临时目录
            if (!files.isEmpty()) {
                String extractedPath = JDBResourceUtils.extractResourceToTemp(JDBResourceUtils.class, files.get(0), null);
                System.out.println("\n文件已提取到: " + extractedPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
